#-*-coding:utf-8-*-
import Image
'''
This module will read the information of the image which is coded by jpg style.
'''

class myJpg:
    '''
    read the jpg module information.
    '''
    def __init__(self):
        self.im = ''        #to restore the Image information
        self.imagedata = '' #to save the pixels.

    def openfile(self,pathname):
        '''
        pathname is the path of the file.
        Open the Image and Judge the type.
        '''
        self.im = Image.open(pathname)
        if self.im.format != 'JPEG':
            return False
        else:
            return True

    def getInfo(self):
        '''
        Get the information of the
        '''
        if self.im:
            return self.im.info
        else:
            return False

    def getStrInfoAndSize(self):
        '''
        get the info and the size in str style.
        '''
        if self.im:
            info = self.im.info
            strout = ''
            for i in info['dpi']:
                strout += (chr(i / 256) +chr(i % 256))
            for i in self.im.size:
                strout += (chr(i / 256) +chr(i % 256))
            return strout
        else:
            return False

    def getInfoAndSize(self,ins):
        '''
        get the size and the dpi
        '''
        ko = list()
        for i in range(0,len(ins),4):
            ko.append((256 * ord(ins[i]) + ord(ins[i + 1]),256 * ord(ins[i + 2]) + ord(ins[i + 3])))
        return ko

    def getData(self):
        '''
        To Get the image data by list.
        '''
        if self.im:
            self.imagedata = self.im.getdata()
            self.info = [self.im.info['dpi'],self.im.size]
            self.mode = self.im.mode
            return True
        else:
            return []       #the image didn't opened

    def putData(self,ldata):
        '''
        put the data into the varitey
        '''
        if self.imagedata:
            self.imagedata.putdata(ldata)
            return True
        else:
            return False

    def tostring(self):
        '''
        get the string data.
        '''
        if self.im:
            self.imagedata = self.im.getdata()
            self.info = [self.im.info['dpi'],self.im.size]
            self.mode = self.im.mode
            return self.im.tostring()
        else:
            return ''

    def SaveAs(self,pathname):
        '''
        Save the Image.
        '''
        if self.im or self.imagedata:
            k = Image.new(self.mode,self.info[1])
            k.putdata(self.imagedata)
            k.save(pathname,dpi = self.info[0],quality = 95)
            return True
        else:
            return False
            
    def SaveLAs(self,pathname,strdata,size):
        '''
        Save the gray Image.
        '''
        k = Image.new('L',size)
        k.putdata(strdata)
        k.save(pathname)

    def test(self):
        '''
        test
        '''
        self.openfile('/home/hucheng/workspace/graduateproject/image-compress-encrypt/res/test.jpg')
        c = self.getData()
        self.SaveAs('/home/hucheng/workspace/graduateproject/image-compress-encrypt/res/11.jpg')


if __name__=='__main__':
    c = myJpg()
    c.test()
